package com.xmtlxx.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.StoreAreaBo;
import com.xmtlxx.wms.domain.vo.StoreAreaVo;
import com.xmtlxx.wms.service.IStoreAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 库区Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "库区控制器", tags = {"库区管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/storeArea")
public class StoreAreaController extends BaseController {

    private final IStoreAreaService iStoreAreaService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<StoreAreaVo> initBean(){
        StoreAreaVo vo = new StoreAreaVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询库区列表
     */
    @ApiOperation("查询库区列表")
//    @SaCheckPermission("wms:storeArea:list")
    @GetMapping("/list")
    public TableDataInfo<StoreAreaVo> list(@Validated(QueryGroup.class) StoreAreaBo bo, PageQuery pageQuery) {
        return iStoreAreaService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询库区列表
     */
    @ApiOperation("查询库区列表")
    @GetMapping("/storeAreas")
    public R<List<StoreAreaVo>> storeAreas(@Validated(QueryGroup.class) StoreAreaBo bo) {
        return R.ok(iStoreAreaService.queryList(bo));
    }

    /**
     * 查询库区列表
     */
    @ApiOperation("查询库区列表")
    @GetMapping("/storeAreasTree")
    public R<List<Tree<String>>> storeAreasTree(@Validated(QueryGroup.class) StoreAreaBo bo) {
        List<StoreAreaVo> items = iStoreAreaService.queryList(bo);
        return R.ok(iStoreAreaService.buildStoreAreaTreeSelect(items));
    }

    /**
     * 导出库区列表
     */
    @ApiOperation("导出库区列表")
    @SaCheckPermission("wms:storeArea:export")
    @Log(title = "库区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated StoreAreaBo bo, HttpServletResponse response) {
        List<StoreAreaVo> list = iStoreAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "库区", StoreAreaVo.class, response);
    }

    /**
     * 获取库区详细信息
     */
    @ApiOperation("获取库区详细信息")
//    @SaCheckPermission("wms:storeArea:query")
    @GetMapping("/{id}")
    public R<StoreAreaVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iStoreAreaService.queryById(id));
    }

    /**
     * 新增库区
     */
    @ApiOperation("新增库区")
    @SaCheckPermission("wms:storeArea:add")
    @Log(title = "库区", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StoreAreaVo vo) {
        StoreAreaBo bo = new StoreAreaBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iStoreAreaService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改库区
     */
    @ApiOperation("修改库区")
    @SaCheckPermission("wms:storeArea:edit")
    @Log(title = "库区", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StoreAreaVo vo) {
        StoreAreaBo bo = new StoreAreaBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iStoreAreaService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除库区
     */
    @ApiOperation("删除库区")
    @SaCheckPermission("wms:storeArea:remove")
    @Log(title = "库区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iStoreAreaService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
